package cn.yr.oss;

import com.qiniu.common.QiniuException;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

import java.io.File;
import java.io.InputStream;
import java.util.Objects;

/**
 * @ClassName : QiniuOss
 * @Description : 七牛云
 * @Author : Lang
 * @Date: 2020-03-07 08:54
 */
public class QnyOss implements Oss {

    /**
     * AccessKey的值
     */
    private String accessKey;
    /**
     * SecretKey的值
     */
    private String secretKey;
    /**
     * 存储空间名
     */
    private String bucket;

    /**
     * 域名
     */
    private String domain;

    private UploadManager uploadManager;

    private BucketManager bucketManager;

    private String token;


    public QnyOss(String accessKey, String secretKey, String bucket, String domain) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
        this.bucket = bucket;

        //加上斜杠结尾方便统一拼接外链
        if (!Objects.isNull(domain) && !domain.endsWith(File.separator)) {
            domain = domain + File.separator;
        }
        this.domain = domain;

        this.token = getToken();

        this.uploadManager = getUploadManager();

        this.bucketManager = getBucketManager();
    }

    @Override
    public String upload(File targetFile) {
        return this.upload(targetFile, targetFile.getName());
    }

    @Override
    public String upload(File targetFile, String resourcesName) {
        try {
            uploadManager.put(targetFile, resourcesName, this.token);
        } catch (QiniuException e) {
            e.printStackTrace();
        }
        return this.domain + resourcesName;
    }

    @Override
    public String upload(String targetName, String resourcesName) {
        try {
            uploadManager.put(targetName, resourcesName, this.token);
        } catch (QiniuException e) {
            e.printStackTrace();
        }
        return this.domain + resourcesName;
    }

    @Override
    public String upload(byte[] data, String resourcesName) {
        try {
            uploadManager.put(data, resourcesName, this.token);
        } catch (QiniuException e) {
            e.printStackTrace();
        }
        return this.domain + resourcesName;
    }

    @Override
    public String upload(InputStream inputStream, String resourcesName) {
        try {
            uploadManager.put(inputStream,resourcesName,this.token,null,null);
        } catch (QiniuException e) {
            e.printStackTrace();
        }
        return this.domain + resourcesName;
    }

    /**
     * @description 权限
     * @date 2020-03-14 11:47
     */
    public Auth getAuth() {
        return Auth.create(this.accessKey, this.secretKey);
    }

    public String getToken() {
        return getAuth().uploadToken(this.bucket);
    }

    public BucketManager getBucketManager() {
        Auth auth = getAuth();
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Region.autoRegion());
        return new BucketManager(auth, cfg);
    }

    /**
     * @description 上传
     * @date 2020-03-14 11:46
     */
    public UploadManager getUploadManager() {
        Configuration cfg = new Configuration(Region.autoRegion());
        return new UploadManager(cfg);
    }


}
